package com.zhangxx.common.mysql;

import com.zaxxer.hikari.HikariDataSource;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;


/**
 * @ClassName MysqlCoreConfig
 * @Description TODO
 * @Author leizhang
 * Date 2020/9/14 9:31 下午
 * @Version 1.0
 **/
@Setter
@Getter
@Configuration
@ConfigurationProperties(prefix = "mysql.core")
@PropertySource(value = {"classpath:mysql-core-jdbc.properties"}, ignoreResourceNotFound = false)
@MapperScan(basePackages = "com.zhangxx.inventory.mappers", sqlSessionFactoryRef = "mysqlCoreSessionFactory")
public class MysqlCoreConfig {
    /**
     * @Description : jdbc路径
     **/
    private String jdbcUrl;
    /**
     * @Description : 连接账号
     **/
    private String jdbcUserName;
    /**
     * @Description : 连接密码
     **/
    private String jdbcPassword;
    /**
     * @Description : 连接驱动
     **/
    private String jdbcDriver;
    /**
     * @Description : mapper文件在classpath下的存放的根路径
     **/
    private String rootMapper;
    /**
     * @Description : 别名包
     **/
    private String aliasesPackage;

    /**
     * @param
     * @return javax.sql.DataSource
     * @Description : 设置连接池
     * @Author : leizhang
     * @Date 10:15 下午 2020/9/14
     **/
    @Bean("mysqlCoreDataSource")
    public DataSource mysqlCoreDataSource() {
        //最快连接池
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setUsername(this.getJdbcUserName());
        dataSource.setPassword(this.getRealPassword());
        dataSource.setJdbcUrl(this.getJdbcUrl());
        dataSource.setDriverClassName(this.getJdbcDriver());
        dataSource.setMaximumPoolSize(40);
        dataSource.setMinimumIdle(3);
        return dataSource;
    }

    /**
     * @Description : 密码翻转
     * @Author : leizhang
     * @Date 10:18 下午 2020/9/14
     * @param
     * @return java.lang.String
     **/
    public String getRealPassword(){
        String jdbcPassword = this.getJdbcPassword();
        String reverse = StringUtils.reverse(jdbcPassword);
        return reverse;
    }
    
    /**
     * @Description : 整合mybatis 创建sqlSessionFactory
     * @Author : leizhang
     * @Date 10:36 下午 2020/9/14
     * @param dataSource
     * @return org.mybatis.spring.SqlSessionFactoryBean
     **/
    @Bean("mysqlCoreSessionFactory")
    public SqlSessionFactoryBean mySqlCoreSessionFactory(@Qualifier("mysqlCoreDataSource") DataSource dataSource) throws Exception{
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        //数据源
        sqlSessionFactoryBean.setDataSource(dataSource);
        //别名
        sqlSessionFactoryBean.setTypeAliasesPackage(this.getAliasesPackage());
        //mapper文件存储的位置
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource[] resources = resolver.getResources(this.getMapperFilePath());
        sqlSessionFactoryBean.setMapperLocations(resources);
        //开启驼峰
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setMapUnderscoreToCamelCase(true);
        sqlSessionFactoryBean.setConfiguration(configuration);
        return sqlSessionFactoryBean;
    }
    
    public String getMapperFilePath(){
        return new StringBuffer("classpath:").append(this.getRootMapper()).append("/**/*.xml").toString();
    }
}
